﻿Imports System.Data.SqlClient

Public Class clsCatalogoAlmacenes
    Private mintIdAlmacen As Integer
    Private mstrcCodigoA01 As String
    Private mstrcNombreA01 As String
    Public Property IdAlmacen As Integer
        Get
            Return mintIdAlmacen
        End Get
        Set(value As Integer)
            mintIdAlmacen = value
        End Set
    End Property
    Public Property cCodigoA01 As String
        Get
            Return mstrcCodigoA01
        End Get
        Set(value As String)
            mstrcCodigoA01 = value
        End Set
    End Property
    Public Property cNombreA01 As String
        Get
            Return mstrcNombreA01
        End Get
        Set(value As String)
            mstrcNombreA01 = value
        End Set
    End Property
    Public Sub New()
        mintIdAlmacen = 0
        mstrcCodigoA01 = ""
        mstrcNombreA01 = ""
    End Sub
    Public Sub Guardar()
        If IdAlmacen = 0 Then
            Exit Sub
        End If

        Dim intRetVal As Integer = BuscarIntDatoCitra("Almacenes", "IdAlmacen", "IdAlmacen=" & IdAlmacen)

        If intRetVal = 0 Then
            Insertar()
        Else
            Actualizar()
        End If
    End Sub

    Public Sub Insertar()
        Dim sql As String = "Insert Into Almacenes (" & _
                            "IdAlmacen," & _
                            "cCodigoA01," & _
                            "cNombreA01)" & _
                            "VALUES (" & _
                            "@IdAlmacen," & _
                            "@cCodigoA01," & _
                            "@cNombreA01)"
                           


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdAlmacen", mintIdAlmacen)
        Command.Parameters.AddWithValue("cCodigoA01", mstrcCodigoA01)
        Command.Parameters.AddWithValue("cNombreA01", mstrcNombreA01)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el almacen " & mstrcNombreA01, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()
        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        strSqlQry = "Update Almacenes Set " & _
                    " cCodigoA01=@cCodigoA01, " & _
                    " cNombreA01=@cNombreA01 " & _
                    " where IdAlmacen=@IdAlmacen"

        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("IdAlmacen", mintIdAlmacen)
        Command.Parameters.AddWithValue("cCodigoA01", mstrcCodigoA01)
        Command.Parameters.AddWithValue("cNombreA01", mstrcNombreA01)

        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el almacen " & mstrcNombreA01, MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
